package com.fire.web.modules.all.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fire.web.modules.all.constant.ErrorCodeConstants;
import com.fire.web.modules.all.entity.AjaxResult;
import com.fire.web.modules.all.entity.FireSupervisorModel;
import com.fire.web.modules.all.mapper.FireSupervisorMapper;
import com.fire.web.modules.all.mapper.SequenceMapper;
import com.fire.web.modules.all.request.AddSupervisorReq;
import com.fire.web.modules.all.service.IFireSupervisorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 监管单位Service
 *
 * @author Matthew
 * @Date 2019-01-10 11:21
 */
@Service
public class FireSupervisorServiceImpl extends ServiceImpl<FireSupervisorMapper, FireSupervisorModel> implements IFireSupervisorService {

	private static final Logger LOGGER = LoggerFactory.getLogger(FireSupervisorServiceImpl.class);

	@Autowired
	private FireSupervisorMapper fireSupervisorMapper;
	@Autowired
	private SequenceMapper sequenceMapper;

	@Override
	public FireSupervisorModel selectOne(FireSupervisorModel entity) {
		return fireSupervisorMapper.selectOne(entity);
	}

	@Override
	public List<FireSupervisorModel> selectList(FireSupervisorModel model) {
		return fireSupervisorMapper.selectList(model, new EntityWrapper<FireSupervisorModel>());
	}

	@Override
	public List<FireSupervisorModel> selectList(FireSupervisorModel model, Wrapper<FireSupervisorModel> wrapper) {
		return fireSupervisorMapper.selectList(model, wrapper);
	}

	@Override
	public List<FireSupervisorModel> selectPage(Pagination pagination, FireSupervisorModel model, Wrapper<FireSupervisorModel> wrapper) {
		return fireSupervisorMapper.selectPage(pagination, model, wrapper);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public AjaxResult addSupervisor(AddSupervisorReq req, String adminid) {
		FireSupervisorModel fireSupervisorModel = new FireSupervisorModel();
		String id = sequenceMapper.getNextStringValue("supervisor");
		fireSupervisorModel.setSupervisorid(id);
		fireSupervisorModel.setUpsuervisorid(req.getUpsuervisorid());
		fireSupervisorModel.setName(req.getName());
		fireSupervisorModel.setAddress(req.getAddress());
		fireSupervisorModel.setLongitude(req.getLongitude());
		fireSupervisorModel.setLatitude(req.getLatitude());
		fireSupervisorModel.setLevation(req.getLevation());
		fireSupervisorModel.setProvince(req.getProvince());
		fireSupervisorModel.setCity(req.getCity());
		fireSupervisorModel.setZone(req.getZone());
		fireSupervisorModel.setDistrict(req.getDistrict());
		fireSupervisorModel.setSupervisortype(req.getSupervisortype());
		fireSupervisorModel.setImageurl(req.getImageurl());
		fireSupervisorModel.setPhone(req.getPhone());
		fireSupervisorModel.setCreatetime(new Date());
		fireSupervisorModel.setUpdatetime(fireSupervisorModel.getCreatetime());
		fireSupervisorModel.setAdminid(adminid);
		fireSupervisorMapper.insert(fireSupervisorModel);
		return new AjaxResult(ErrorCodeConstants.SUCCESS, "新增成功", "新增成功");
	}

	@Override
	public AjaxResult updateSupervisor(AddSupervisorReq req) {
		FireSupervisorModel fireSupervisorModel = new FireSupervisorModel();
		fireSupervisorModel.setUpsuervisorid(req.getUpsuervisorid());
		fireSupervisorModel.setName(req.getName());
		fireSupervisorModel.setAddress(req.getAddress());
		fireSupervisorModel.setLongitude(req.getLongitude());
		fireSupervisorModel.setLatitude(req.getLatitude());
		fireSupervisorModel.setLevation(req.getLevation());
		fireSupervisorModel.setProvince(req.getProvince());
		fireSupervisorModel.setCity(req.getCity());
		fireSupervisorModel.setZone(req.getZone());
		fireSupervisorModel.setDistrict(req.getDistrict());
		fireSupervisorModel.setSupervisortype(req.getSupervisortype());
		fireSupervisorModel.setImageurl(req.getImageurl());
		fireSupervisorModel.setPhone(req.getPhone());
		fireSupervisorModel.setCreatetime(new Date());
		fireSupervisorModel.setUpdatetime(fireSupervisorModel.getCreatetime());
		fireSupervisorMapper.update(fireSupervisorModel, new EntityWrapper<FireSupervisorModel>().eq("supervisorid", req.getSupervisorid()));
		return new AjaxResult(ErrorCodeConstants.SUCCESS, "更新成功", "更新成功");
	}

	@Override
	public AjaxResult selectLabelValue(String supervisorid) {
		List<Map> result = fireSupervisorMapper.selectLabelValue(supervisorid);
		return new AjaxResult(ErrorCodeConstants.SUCCESS, "监管单位下拉选查询成功", result, "监管单位下拉选查询成功");
	}

	@Override
	public Page<Map<String, Object>> querySupervisorListMap(Page page, Wrapper wrapper) {
		wrapper = SqlHelper.fillWrapper(page, wrapper);
		page.setRecords(fireSupervisorMapper.querySupervisorListMap(page, wrapper));
		return page;
	}

	/**
	 * 根据消防单位ID查询该单位管理的所有消防单位，包括自己
	 * @param supervisorid
	 * @return
	 */
	@Override
	public List<FireSupervisorModel> getSupervisorListById(String supervisorid){
		return fireSupervisorMapper.getSupervisorListById(supervisorid);
	}

}
